package dao.impl;

import dao.BillDao;
import entry.User;
import pojo.Amount;
import pojo.TransferTable;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class BillDaoImpl implements BillDao {
    private DBHelper db=new DBHelper();

    @Override
    public ArrayList<User> selectUser(String account, String password) {
        String sql="select * from user where account=? and password=?";
        return db.query(sql,User.class,account,password);
    }

    @Override
    public ArrayList<TransferTable> selectLogin() {
        String sql="select t.no,t.amount,t.opentime,t.transfertime,t.expirytime,t.uplinkaddress,t.status,t.progressstatus,e.companyname,ea.companyname as ename,i.bankname\n" +
                "FROM enterprise e \n" +
                "INNER JOIN transfer t ON t.receiveenterpriseid=e.id\n" +
                "INNER JOIN instituty i ON t.institutyid=i.id\n" +
                "INNER JOIN enterprise ea on t.openenterpriseid=ea.id";
        return db.query(sql,TransferTable.class);
    }

    @Override
    public ArrayList<TransferTable> selectQuery(TransferTable transferTable, Amount amount, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder("select t.no,t.amount,t.opentime,t.transfertime,t.expirytime,t.uplinkaddress,t.status,t.progressstatus,e.companyname ,ea.companyname as ename,i.bankname\n" +
                "FROM enterprise e \n" +
                "INNER JOIN transfer t ON t.receiveenterpriseid=e.id\n" +
                "INNER JOIN instituty i ON t.institutyid=i.id\n" +
                "JOIN enterprise ea on t.openenterpriseid=ea.id where 1=1 ");
        ArrayList list = new ArrayList();
        if (transferTable.getNo() != null&&transferTable.getNo().length()>0) {
            sql.append(" and no like ? ");
            list.add("%"+transferTable.getNo()+"%");
        }
        if (transferTable.getOpentime()!=null&&transferTable.getOpentime().length()>0) {
            sql.append(" and opentime like ? ");
            list.add("%"+transferTable.getOpentime()+"%");
        }
        if (transferTable.getCompanyname() != null&&transferTable.getCompanyname().length()>0) {
            sql.append(" and e.companyname like ? ");//收单方企业名
            list.add("%" + transferTable.getCompanyname() + "%");
        }
        if (transferTable.getEname() != null&&transferTable.getEname().length()>0) {
            sql.append(" and ea.companyname like ? ");//开单方企业名
            list.add("%" + transferTable.getEname() + "%");
        }
        if(transferTable.getStatus()!=null){
            sql.append(" and status=? ");
            list.add(transferTable.getStatus());
        }
        if(amount.getMinAcount()!=null&&amount.getMaxAcount()!=null){
            sql.append(" and amount BETWEEN ? and  ? ");
            list.add(amount.getMinAcount());
            list.add(amount.getMaxAcount());
        }
        sql.append(" limit ?,?");
        int startIndex = (pageHelper.getIndexPage() - 1) * pageHelper.getShowData();
        list.add(startIndex);
        list.add(pageHelper.getShowData());
        return db.query(sql.toString(), TransferTable.class, list.toArray());
    }

    /*public static void main(String[] args) {
        Amount amount=new Amount();
        TransferTable transferTable = new TransferTable();
        //transferTable.setNo("1");
        transferTable.setCompanyname("腾讯科技有限公司");
        //transferTable.setEname("百度科技有限公司");
       *//* transferTable.setOpentime("2024-08-18");*//*
        PageHelper pageHelper = new PageHelper();
        *//*amount.setMinAcount(10000);
        amount.setMaxAcount(70000);*//*
        System.out.println(new BillDaoImpl().selectQuery(transferTable,amount, pageHelper));
        //System.out.println(new BillDaoImpl().selectLogin());
    }*/
}
